﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Util;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
/// <summary>
/// Summary description for Favorites
/// </summary>
namespace DAL
{
    public class Favorites
    {
        public bool addFavorite(Favorite favorite)
        {
            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = ConfigurationManager.ConnectionStrings["FProj"].ConnectionString;

                String strSQL = "INSERT INTO Favorites (FavoriteURI, UserID, FavoriteName) VALUES (@favURI, @userId,@favName)";
                SqlCommand comm = new SqlCommand(strSQL, con);

                SqlParameter param = new SqlParameter();
                param.ParameterName = "@favURI";
                param.Value = favorite.FavoriteURI;
                param.SqlDbType = SqlDbType.NVarChar;
                comm.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@userId";
                param.Value = favorite.UserID;
                param.SqlDbType = SqlDbType.NVarChar;
                comm.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@favName";
                param.Value = favorite.FavoriteName;
                param.SqlDbType = SqlDbType.NVarChar;
                comm.Parameters.Add(param);

                con.Open();

                try
                {
                    comm.ExecuteNonQuery();
                }
                catch (System.Data.SqlClient.SqlException)
                {
                    return false;
                }
            }
            return true;
        }

        public List<Favorite> getFavoriteOfUser(string userID)
        {
            SqlDataReader sdr;
            List<Favorite> favList = new List<Favorite>();

            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = ConfigurationManager.ConnectionStrings["FProj"].ConnectionString;
                con.Open();
                String strSQL = "SELECT * FROM Favorites WHERE UserID = @userId";
                SqlCommand comm = new SqlCommand(strSQL, con);

                SqlParameter param = new SqlParameter();
                param.ParameterName = "@userId";
                param.Value = userID;
                param.SqlDbType = SqlDbType.NVarChar;
                comm.Parameters.Add(param);

                sdr = comm.ExecuteReader(CommandBehavior.CloseConnection);

                if (sdr != null)
                {
                    while (sdr.Read())
                    {
                        favList.Add(new Favorite() { FavoriteURI = sdr["FavoriteURI"].ToString(),  FavoriteID = sdr["FavoriteID"].ToString(), UserID = sdr["UserID"].ToString(), FavoriteName=sdr["FavoriteName"].ToString() });
                    }
                }
            }
            return favList;
        }

        public Favorite getFavorite(string favId)
        {
            SqlDataReader sdr;
            Favorite favorite = null;

            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = ConfigurationManager.ConnectionStrings["FProj"].ConnectionString;
                con.Open();
                String strSQL = "SELECT * FROM Tracks WHERE FavoriteID = @favID";
                SqlCommand comm = new SqlCommand(strSQL, con);

                SqlParameter param = new SqlParameter();
                param.ParameterName = "@favID";
                param.Value = favId;
                param.SqlDbType = SqlDbType.UniqueIdentifier;
                comm.Parameters.Add(param);

                sdr = comm.ExecuteReader(CommandBehavior.CloseConnection);

                if (sdr != null)
                {
                    while (sdr.Read())
                    {
                        favorite = new Favorite() { FavoriteURI = sdr["FavoriteURI"].ToString(), FavoriteID = sdr["FavoriteID"].ToString(), UserID = sdr["UserID"].ToString(), FavoriteName = sdr["FavoriteName"].ToString() };
                    }
                    return favorite;
                }

            }

            return null;
        }
    }
}